/**
 * fileName: GridTag.java
 *     Date: 2012-5-1
 **/
package com.stranger.common.tag.grid;


import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;


/**
 * @作者 stranger.lin
 *
 * @时间 2012-5-1
 */
public class GridTag extends TagSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = -4639219012846539131L;
	private String id;
	private String url;
	private boolean multiselect;
	private String caption;
	private String height="auto";
	private String onComplete;
	private List<Column> columns;
	private 	JspWriter out;
	private String onSelectRow;
	private String onDbSelectRow;
	
	/**
	 *  <table id="jsonmap" width="100%"></table>
 	 *	<div id="pjmap"></div>
	 * 
	 * */
	@Override
	public int doStartTag() throws JspException {
		try {
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return EVAL_BODY_INCLUDE;
	}
	@Override
	public int doEndTag() throws JspException {
		try {
			out=pageContext.getOut();
			if (url==null) {
				throw new JspException("url 不能为空");
			}
			if (id==null) {
				throw new JspException("Id 不能为空");
			}
		
			String tableId=id;
			String tableFooterId="pjmap_"+UUID.randomUUID();
			StringBuffer html=new StringBuffer();
			html.append("<table id=\""+tableId+"\" width=\"100%\"></table>");
			html.append("<div id=\""+tableFooterId+"\"></div>");
			html.append("<script>$(function(){");
			html.append("$(\"#"+tableId+"\").jqGrid({\n url:\""+url+"\", \n ");
			html.append("datatype: \"json\",\n");
			//Tag flg= findAncestorWithClass(this, GridColumnTag.class);
			if (columns!=null) {
				StringBuffer headerNamesBuffer=new StringBuffer("colNames:[");
				StringBuffer nameBuffer=new StringBuffer("colModel:[");
				for (Column column : columns) {
					headerNamesBuffer.append("\""+column.getHeaderName()+"\",");
					nameBuffer.append("{name:\""+column.getName()+"\",index:\""+column.getName()+"\"," +
							"hidden:"+isHidden(column.getHidden())+","
							+bufferHtml(column.getJsonmap())+
							"width:"+column.getWith()+",align:\""+alignFun(column.getAlign())+
							"\",sortable:"+isSortable(column.getSortable())+"},");
				}
				String header=headerNamesBuffer.subSequence(0, headerNamesBuffer.length()-1).toString();
				headerNamesBuffer.setLength(0);
				headerNamesBuffer=new StringBuffer(header);
				headerNamesBuffer.append("],");
				
				String name=nameBuffer.subSequence(0,nameBuffer.length()-1).toString();
				nameBuffer.setLength(0);
				nameBuffer=new StringBuffer(name);
				nameBuffer.append("],");
				html.append(headerNamesBuffer+"\n");
				html.append(nameBuffer+"\n");
			}
			
			html.append("rowNum:10,\n rowList:[10,20,30],\n");
			html.append("pager: \"#"+tableFooterId+"\",\n");
			html.append(" viewrecords: true,\n");
			html.append(" sortorder: \"desc\",\n");
			if (onComplete!=null&&!"".equals(onComplete.trim())) {
				html.append("loadComplete:function(){"+onComplete+"()},");
			}
			if (onSelectRow!=null&&!"".equals(onSelectRow.trim())) {
				html.append("onSelectRow:function(ids){var row = $(\"#"+id+"\").jqGrid('getRowData',ids);\n"+onSelectRow+"(row);},");
			}
			if (onDbSelectRow!=null&&!"".equals(onDbSelectRow.trim())) {
				html.append("ondblClickRow:function(ids){var row = $(\"#"+id+"\").jqGrid('getRowData',ids);\n"+onDbSelectRow+"(row);},");
			}
			
			html.append("jsonReader: {repeatitems : false,id: \"0\"},\n");
			html.append("multiselect: "+multiselect+",\n");
			html.append("caption: \""+caption+"\",\n height: '"+height+"'});");
			html.append("});</script>");
			out.print(html);
			if (out!=null) {
				out.flush();
			}
			columns=null;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return EVAL_PAGE;
	}

	private String bufferHtml(String jsonmap){
		if (jsonmap!=null&&!"".equals(jsonmap)) {
			return "jsonmap:\""+jsonmap+"\",";
		}
		return "";
	}

	private boolean isHidden(String hidden){
		if (hidden!=null&&!"".equals(hidden)) {
			if ("true".equals(hidden)) {
				return true;
			}
		}
		return false;
	}

	private String alignFun(String align){
		if (align!=null&&!"".equals(align.trim())) {
			return align;
		}
		return "left";
	}
	
	private String isSortable(String sort){
		if (sort!=null&&!"".equals(sort.trim())) {
			return sort;
		}
		return "true";
	}
	
	public String getUrl() {
		return url;
	}


	public void setUrl(String url) {
		this.url = url;
	}


	public boolean isMultiselect() {
		return multiselect;
	}


	public void setMultiselect(boolean multiselect) {
		this.multiselect = multiselect;
	}


	public String getCaption() {
		return caption;
	}


	public void setCaption(String caption) {
		this.caption = caption;
	}
	public List<Column> getColumns() {
		if (columns==null) {
			columns=new ArrayList<Column>();
		}
		return columns;
	}
	public void setColumns(List<Column> columns) {
		this.columns = columns;
	}
	public String getHeight() {
		return height;
	}
	public void setHeight(String height) {
		this.height = height;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getOnComplete() {
		return onComplete;
	}
	public void setOnComplete(String onComplete) {
		this.onComplete = onComplete;
	}
	public String getOnSelectRow() {
		return onSelectRow;
	}
	public void setOnSelectRow(String onSelectRow) {
		this.onSelectRow = onSelectRow;
	}
	public String getOnDbSelectRow() {
		return onDbSelectRow;
	}
	public void setOnDbSelectRow(String onDbSelectRow) {
		this.onDbSelectRow = onDbSelectRow;
	}


	
	

}
